package com.sdp.right.dr;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.sdp.core.bizc.db.QueryColumn;
import com.sdp.core.bizc.exception.ApplicationException;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.right.cache.keys.RightCacheKeys;
import com.sdp.right.db.sys_dict.service.ISysDictService;
import com.sdp.right.entity.SysDrEmpRule;
import com.sdp.right.entity.SysDrRoleRule;

@Component
public class DrFilter {
	
	@Autowired
	private ISysDictService iSysDictService;

	
	@SuppressWarnings("unchecked")
	public void empFilter(DataCenter dc,String drNo,Object drEmpRuleCache) {
		List<SysDrEmpRule> ruleList = (List<SysDrEmpRule>) drEmpRuleCache;
		
		List<String> rules = new ArrayList<>();
		for(SysDrEmpRule r : ruleList) {
			rules.add(r.getDrRule());
		}
		
		drFilter(dc,rules);
	}
	
	
	@SuppressWarnings("unchecked")
	public void roleFilter(DataCenter dc,String drNo,String accountNo) {
		List<Object> personRoleNo = iSysDictService.getCacheDictInfo(RightCacheKeys.PERSON_ROLE_NO,accountNo);
		if(personRoleNo==null || personRoleNo.size()==0) {
			return;
		}
		
		List<String> roleNos = new ArrayList<>();
		for(int i=0;i<personRoleNo.size();i++) {
			roleNos.add(personRoleNo.get(i).toString());
		}
		//根据数据权限编号获取配置信息
		Object drRuleCache = iSysDictService.getCacheDictInfo(RightCacheKeys.DR_ROLE_CACHE_KEY, drNo);
		if(drRuleCache==null) {
			return;
		}
		//交集
		List<String> intersection = null;
		List<SysDrRoleRule> ruleList = (List<SysDrRoleRule>) drRuleCache;
		//符合当前登录人所属角色的规则
		List<SysDrRoleRule> newRuleList = new ArrayList<>();
		
		for(SysDrRoleRule rule : ruleList) {
			intersection = new ArrayList<>(roleNos);
			intersection.retainAll(new ArrayList<String>(Arrays.asList(rule.getRoleNos().split(","))));
			if(intersection.size()>0) {
				newRuleList.add(rule);
			}
		}
		
		if(newRuleList.size()==0) {
			return;
		}
		
		List<String> rules = new ArrayList<>();
		for(SysDrRoleRule r : newRuleList) {
			rules.add(r.getDrRule());
		}
		
		drFilter(dc,rules);
	}
	
	public void drFilter(DataCenter dc,List<String> rules) {
		List<QueryColumn> qcList = dc.getQueryForm();
		if(qcList==null) {
			qcList = new ArrayList<>();
			dc.setQueryForm(qcList);
		}
		JSONArray ruleArray = new JSONArray();
		for(String r : rules) {
			ruleArray = JSONArray.parseArray(r);
			for(int i=0;i<ruleArray.size();i++) {
				QueryColumn qc = new QueryColumn();
				qc.setField(ruleArray.getJSONObject(i).getString("field"));
				qc.setCondition(ruleArray.getJSONObject(i).getString("condition"));
				
				if(!StringUtils.isEmpty(ruleArray.getJSONObject(i).getString("value"))) {
					qc.setVal(ruleArray.getJSONObject(i).getString("value"));
				}else {
					try {
						Class<?> c = Class.forName(ruleArray.getJSONObject(i).getString("dataConstructor"));
						Method method= c.getDeclaredMethod("getData");
						qc.setVal(method.invoke(ApplicationContextUtils.getBean(c)).toString());
					} catch (Exception e) {
						e.printStackTrace();
						throw new ApplicationException(e.getMessage());
					}
				}
				qcList.add(qc);
			}
		}	
	}
}
